
Imports System.Data.SqlClient
Imports System.Console
Imports System.IO.TextReader
Imports System.IO
Imports System.Net
Imports System.IO.Ports
Imports System.Text
Imports System.Xml
Imports System.IO.FileStream
Imports AVLProject.GeoPoint
Imports AVLProject.DeclaracionesFunciones

Imports System.Threading
Imports System.Collections


Public Class FrmPrincipal

    Private WithEvents SerialPort As New IO.Ports.SerialPort
    Dim LogSerialPort As New StreamWriter("C:\nmea0183.txt", True)
    Dim Linea As String
    Dim Linea2 As String
    Dim GPS As New GPSDevice
    Dim Datos As New Stack
    Dim Habilitado As Boolean = True



    Private Sub FrmPrincipal_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        TimerCheck.Interval = 1000
        TimerCheck.Enabled = True
        Button1.Text = "Stop Update View"

        Dim StringConecta As String = "User Id=sa2;password=1234;Data Source=127.0.0.1\SQLEXPRESS;Initial Catalog=TEST"
        Dim Conecta As New SqlConnection(StringConecta)
        Conecta.Open()





        Dim Tarea1 As New Thread(AddressOf GrabarNmeaFile)
        Tarea1.IsBackground = True
        Tarea1.Start()









    End Sub



    Public Sub GrabarNmeaFile()


        Dim GPS As New GPSDevice

        Do
            Dim PilaDataGPS As Stack = GPS.ReadData(PortCOM)
            For i As Integer = 0 To PilaDataGPS.Count - 1
                Dim DatoGPS As String = PilaDataGPS.Pop()
                LogSerialPort.Write(DatoGPS & vbCrLf)
                If DatoGPS.Substring(0, 6) = "$GPRMC" Then
                    GrabarDataBasePoint(DatoGPS)
                End If
            Next
            PilaDataGPS.Clear()
            Thread.Sleep(800)
        Loop


        'Dim consulta As GeoWayPoints
        'Dim s As GeoPoint

        'consulta.Add("1", "3", "6", "6", "6", "6")



        'Do
        '    Dim GPSFile As New GPSNmeaFile
        '    GPSFile.GrabarRegistroFile()
        'Loop

    End Sub


    Public Sub GrabarDataBasePoint(ByVal DatoGPS As String)

        Dim Punto As New GeoPoint
        Dim FieldsDataGPS() As String = Split(DatoGPS, ",")

        Dim Mobil1 As Integer = IDMovil
        Dim Fecha As String = FieldsDataGPS(9)
        Dim Hora As String = FieldsDataGPS(1)
        Dim Latitud As String = FieldsDataGPS(3)
        Dim Longitud As String = FieldsDataGPS(5)
        Dim Velocidad As String = FieldsDataGPS(7)
        Dim Rumbo As String = FieldsDataGPS(8)

        Dim Validate As String = FieldsDataGPS(2)

        Dim HemisphereSN As String = FieldsDataGPS(6)
        Dim HemisphereEW As String = FieldsDataGPS(8)

        'Dim Checksum As String = FieldsDataGPS(12)

        Punto.Grabar(Mobil1, Fecha, Hora, Latitud, Longitud, Velocidad)

        
    End Sub


    Public Sub ActualizarDataGrid()

        Dim Punto As New GeoPoint
        Dim DataSetPoints As DataSet = Punto.ReadLastPointsUpdated()
        DataGridView1.DataSource = DataSetPoints.Tables("ListarPuntos")

        Dim p As New GeoPoint
        Dim g As GeoPoint = p.LastPointUpdated

        Label1.Text = g.Fecha
        Label8.Text = g.HoraUTC
        Label3.Text = g.Latitud
        Label2.Text = g.longitud



    End Sub



    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click


        If Habilitado = True Then

            TimerCheck.Enabled = True
            Button1.Text = "Stop Update View"

        Else

            TimerCheck.Enabled = False
            Button1.Text = "Star Update View"

        End If


        Habilitado = Not (Habilitado)


        
    End Sub

    
    
    Private Sub TimerCheck_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TimerCheck.Tick


        ActualizarDataGrid()



    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click


        Dim FormularioConsulta As New FrmConsulta
        FormularioConsulta.Show()


    End Sub
End Class
